var app = getApp();
Page({
    data:{
        isIPX : app.globalData.isIPX,
        page : {
            isend : false,
            doing : false,
            doo : 'ctagent',
            pdata : {
                op : 'datalist',
                page : 1,
                showtype : 1,
                otype : 'id',
                odesc : 1,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onShow(){
        if( this.data.needback ){
            wx.navigateBack();
        }
    },
    onLoad:function(options){
        var self = this;
        
        self.setData({
            options : options,
        });
        app.com.auth(function(user){

            app.com.getPage(self,self.data.page,20,function(res){
                self.setData({
                    datalist : res.data.data.list,
                });
            });

            app.com.http('ctagent','POST',{op:'index'},0,false,'','',function(res){
                if (!res.data.errno) {
                    self.setData({
                        Pagetheme : res.data.data.params.style,
                        userinfo : res.data.data.userinfo,
                        feedata : res.data.data.feedata,
                        inited : true,
                    });
                    // 关闭下拉刷新
                    wx.stopPullDownRefresh();

                }else if( res.data.errno == 3 ){
                    self.setData({
                        needback : true,
                    }) 
                    wx.navigateTo({
                        url : '/zofui_sales/ctagent/join'
                    });
                }else{
                    app.com.alert(res.data.message);
                }
            });
        });
        wx.setNavigationBarTitle({
          title:  '代理',
        });
       
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
        });
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.article.title,
            path: '',
            imageUrl : self.data.article.img,
        }
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    tabChange : function(e){
        var self = this;
        if( self.data.ising ||  e.currentTarget.dataset.type == self.data.page.pdata.showtype ) return false;
        self.data.ising = true;

        this.setData({
            'page.pdata.showtype' : e.currentTarget.dataset.type,
            'page.pdata.page' : 1,
            'page.pdata.otype' : 'id',
            'page.pdata.odesc' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0,
            datalist : [],
        })

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;

    },
    changeorder : function(e){
        var self = this;

        if( self.data.ising ||  e.currentTarget.dataset.odesc == self.data.page.pdata.odesc ) return false;
        self.data.ising = true;

        var now = this.data.page.pdata.odesc == 1 ? 0 : 1;
        this.setData({
            'page.pdata.otype' : e.currentTarget.dataset.otype,
            'page.pdata.page' : 1,
            'page.pdata.odesc' : now,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0,
        })

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;
    },
    uplevel : function(){
        var self = this;
        var postdata = {
            op : 'subjoin',
            level : self.data.userinfo.cttype == 1 ? 2 : 3,
        };

        app.com.http('ctagent','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data ) {
                app.com.alert(res.data.message);
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },

})